from apps.pay.recordIncomesLocked.baseViews import *
from apps.pay.recordIncomes.dao import RecordIncomesDao
from apps.user.dao import UserDao
from apps.pay.order.dao import OrderDao

class RecordIncomesLockedViewSet(BaseRecordIncomesLockedViewSet):

    def get_queryset(self):
        params = getRequestParams(self.request)

        _list = RecordIncomesLockedDao().findAll().select_related('user', 'fromUser')
        if self.request.user.role.key == 'custom':
            _list = _list.filter(user=self.request.user)
        return _list

    @action(methods=['post'], detail=False)
    def properayHandle(self, request):
        params = getRequestParams(self.request)
        _user = request.user
        obj_id = params.get('obj_id')
        _properay = RecordIncomesLockedDao().findOneByObjId(obj_id)
        if _properay.orderStatus == 'LOCKED':
            return Response(response_message('资产未解锁，无法处置！'))
        if _properay.orderStatus == 'FINISH':
            return Response(response_message('无法操作，资产已处置！'))

        if params.get('handleType') == '1':  # 到期发货
            RecordIncomesLockedDao().update(obj_id, {'orderStatus': 'FINISH', 'handleType': '1'})
            # 释放延时锁定积分
            _money = _properay.money2
            UserDao().addWallet('incomes', _user, _money)
            data = {}
            data['lessee'] = _user.lessee.id
            data['scene'] = 'unlockedIncomes'
            data['user'] = _user.id
            data['fromUser'] = _user.id
            data['orderId'] = ''
            data['money'] = _money
            data['payway'] = 'incomesLocked'
            data['changeWay'] = 'add'
            data['orderStatus'] = 'FINISH'
            RecordIncomesDao().create(data)
        elif params.get('handleType') == '2':  # 订单转让
            RecordIncomesLockedDao().update(obj_id, {'orderStatus': 'FINISH', 'handleType': '2'})
            # 释放本金和延时锁定积分
            _money = _properay.money + _properay.money2
            UserDao().addWallet('incomes', _user, _money)
            data = {}
            data['lessee'] = _user.lessee.id
            data['scene'] = 'unlockedIncomes'
            data['user'] = _user.id
            data['fromUser'] = _user.id
            data['orderId'] = ''
            data['money'] = _money
            data['payway'] = 'incomesLocked'
            data['changeWay'] = 'add'
            data['orderStatus'] = 'FINISH'
            RecordIncomesDao().create(data)
            # 修改订单状态为完成已完成
            _order = OrderDao().findOneByOrderId(_properay.orderId)
            OrderDao().update(_order.id, {'orderStatus': 'FINISH'})
        elif params.get('handleType') == '3':  # 退货退款
            RecordIncomesLockedDao().update(obj_id, {'orderStatus': 'FINISH', 'handleType': '3'})
            # 释放本金和延时锁定积分
            _money = _properay.money + _properay.money2
            UserDao().addWallet('incomes', _user, _money)
            data = {}
            data['lessee'] = _user.lessee.id
            data['scene'] = 'unlockedIncomes'
            data['user'] = _user.id
            data['fromUser'] = _user.id
            data['orderId'] = ''
            data['money'] = _money
            data['payway'] = 'incomesLocked'
            data['changeWay'] = 'add'
            data['orderStatus'] = 'FINISH'
            RecordIncomesDao().create(data)
            # 修改订单状态为完成已完成
            _order = OrderDao().findOneByOrderId(_properay.orderId)
            OrderDao().update(_order.id, {'orderStatus': 'FINISH'})
        return Response(response_message("操作成功"))
